package com.example.news.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.news.R;
import com.example.news.adapter.NewsListAdapter;
import com.example.news.bean.NewsListJson;
import com.example.news.utils.ImageLoaderUtils;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.ArrayList;

public class DetailsActivity extends FragmentActivity {

    private ListView lv;
    private String path = "http://api.avatardata.cn/QiWenNews/Query?key=ac3badd7a5c2497d80b98559c57bf114&rows=10&page=";
    private int pages = 1;
    private NewsListAdapter adapter;
    ArrayList<NewsListJson.MyResult> list = new ArrayList<NewsListJson.MyResult>();
    private DisplayImageOptions options;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隐藏系统自带的标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_details);

        String name = getIntent().getStringExtra("title");

        //找控件
        lv = (ListView) findViewById(R.id.details_lv);
        TextView title = (TextView) findViewById(R.id.details_title);

        title.setText(name);

        options = ImageLoaderUtils.initOptions();

        getData(1);
    }

    /*
     *  定义请求网络的方法
     */
    public void getData(final int page) {
        new Thread() {
            @Override
            public void run() {
                HttpUtils utils = new HttpUtils();
                utils.send(HttpRequest.HttpMethod.GET, path + page, new RequestCallBack<String>() {
                    @Override
                    public void onFailure(HttpException e, String s) {
                            Toast.makeText(DetailsActivity.this, "当前网络不可用", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String json = responseInfo.result;
                        Gson gson = new Gson();
                        //解析json字符串
                        NewsListJson newsListJson = gson.fromJson(json, NewsListJson.class);

                        list = newsListJson.result;


                        adapter = new NewsListAdapter(DetailsActivity.this, list, options);
                        lv.setAdapter(adapter);

                        //设置条目点击监听
                        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                int index = position - 1;
                                //跳转到详情界面
                                Intent intent = new Intent(DetailsActivity.this, WebActivity.class);
                                intent.putExtra("url", list.get(index).url);
                                intent.putExtra("title", list.get(index).title);
                                startActivity(intent);
                            }
                        });
                    }
                });
            }
        }.start();
    }

    /*
     * 返回按钮
     */
    public void close(View v){
        finish();
    }
}
